<%@ page language="java" import="java.util.*,com.demodis.domain.TblUser" pageEncoding="UTF-8"%>
<%
	int cuUserId = ((TblUser)request.getSession().getAttribute("user")).getUserId();
	String deterUrl = ((TblUser)request.getSession().getAttribute("user")).getDeterUrl();
	String cUserName = ((TblUser)request.getSession().getAttribute("user")).getUsername();
	String tools = "";
	String action = "";
	if("admin".equalsIgnoreCase(cUserName)) {
		tools = "{text : '增加',iconCls : 'icon-add',handler : function() {moduls_deterPerson_addFun();}}, '-', {text : '批量删除',iconCls : 'icon-remove',handler : function() {moduls_deterPerson_removeFun();}}, '-',";
		action = "";
	}
	if(deterUrl == null) {
		deterUrl = "null";
	}
	String queryDeterStat = deterUrl;
%>
<script type="text/javascript" charset="UTF-8">
var deterPerson_QueryDetDetailed = '<%= deterUrl%>';
var moduls_deterPerson_propertyGrid;
$(function() {
	$('#moduls_deterPerson_propertyGrid').datagrid({
		url : 'deterPersonAction!query.action',
		fit : true,
		fitColumns : false,
		checkOnSelect : false,
		selectOnCheck : false,
		singleSelect : true,
		idField : 'dpId',
		pagination : true,
		border : false,
		toolbar : '#moduls_deterPerson_datagrid_toolbar',
		columns : [ [ {
			title : '个人测评ID',
			field : 'dpId',
			width : 50,
			checkbox : true
		}, {
			title : '部门ID',
			field : 'groupId',
			width : 50,
			hidden : true
		}, {
			title : '分管领导ID',
			field : 'groupLeaderId',
			width : 50,
			hidden : true
		}, {
			title : '被测评者姓名',
			field : 'userRealName',
			width : 100
		}, {
			title : '测评者上级',
			field : 'personSup',
			width : 100,
			formatter : function(value, row, index) {
				if (row.id == '0') {
					return '';
				} else {
					return row.supName;
				}
			}
		}, {
			title : '测评者下级',
			field : 'personSub',
			width : 100,
			formatter : function(value, row, index) {
				if (row.id == '0') {
					return '';
				} else {
					return row.subName;
				}
			}
		}, {
			title : '所属岗位',
			field : 'posName',
			width : 100
		}, {
			title : '测评者岗位',
			field : 'posId',
			width : 100,
			hidden : true,
			formatter : function(value, row, index) {
				if (row.id == '0') {
					return '';
				} else {
					return row.posName;
				}
			}
		}, {
			title : '所属部门',
			field : 'groupName',
			width : 130
		}, {
			title : '责任对象',
			field : 'dutyObj',
			width : 100
		}, {
			field : 'action',
			title : '动作',
			width : 125,
			formatter : function(value, row, index) {
				var userId = <%= cuUserId%>;
				if (row.userId === userId) {
					return formatString('<img style="display: <%="admin".equalsIgnoreCase(cUserName)?"" : "none" %>;" onclick="moduls_deterPerson_editFun(\'{0}\');" src="{1}"/><%="admin".equalsIgnoreCase(cUserName)?"&nbsp;&nbsp;&nbsp;" : "" %><img style="display: <%="admin".equalsIgnoreCase(cUserName)?"" : "none" %>;" onclick="moduls_deterPerson_deleteFun(\'{2}\');" src="{3}"/><%="admin".equalsIgnoreCase(cUserName)?"&nbsp;&nbsp;&nbsp;" : "" %>', row.dpId, 'jslib/jquery-easyui-1.3.1/themes/icons/pencil.png', row.dpId, 'jslib/jquery-easyui-1.3.1/themes/icons/cancel.png');
				} else {
					return formatString('<img style="display: <%="admin".equalsIgnoreCase(cUserName)?"" : "none" %>;" onclick="moduls_deterPerson_editFun(\'{0}\');" src="{1}"/><%="admin".equalsIgnoreCase(cUserName)?"&nbsp;&nbsp;&nbsp;" : "" %><img style="display: <%="admin".equalsIgnoreCase(cUserName)?"" : "none" %>;" onclick="moduls_deterPerson_deleteFun(\'{2}\');" src="{3}"/><%="admin".equalsIgnoreCase(cUserName)?"&nbsp;&nbsp;&nbsp;" : "" %><img onclick="moduls_deterPerson_doDeterPerFun(\'{4}\', \'{5}\', \'{6}\');" src="{7}"/>', row.dpId, 'jslib/jquery-easyui-1.3.1/themes/icons/pencil.png', row.dpId, 'jslib/jquery-easyui-1.3.1/themes/icons/cancel.png', row.dpId, row.userId, row.posType, 'jslib/jquery-easyui-1.3.1/themes/icons/book_edit.png');
				}
			}
		} ] ]
	});
	initPostListMenu();
	
	if(treeMenuData.length < 4) {
		$('<div/>').dialog({
			href : '${pageContext.request.contextPath}/moduls/fun/opguild.html',
			width : 550,
			height : 400,
			modal : true,
			title : '操作使用说明',
			buttons : [ {
				text : '关闭',
				iconCls : 'icon-remove',
				handler : function() {
					$(this).closest('.window-body').dialog('destroy');
				}
			} ],
			onClose : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		});
	}
});

function initPostListMenu() {
	$.ajax({
		type : 'POST',
		url : 'positionAction!query.action',
		cache : false,
		dataType : 'JSON',
		success : function(data) {
			for(var i = 0; i < data.rows.length; i++) {
				if(data.rows[i].posId) {
					
				}
				$('div[id=postList]').menu('appendItem', {
		    		id: data.rows[i].posId,
		    		text: data.rows[i].posName,
		    		iconCls: 'icon-group'
		    	});
			}
		}
	});
	
	$("div[id='postList']").menu({   
	    onClick:function(item){   
	    	$('#moduls_deterPerson_propertyGrid').datagrid('load', {
	        	posId : item.id == undefined ? 0 : item.id
	        });   
	    }   
	}); 
}

var moduls_deterPerson_doDeterPerFun_id;
var moduls_deterPerson_doDeterPerFun_userId;
var moduls_deterPerson_doDeterPerFun_div;
var moduls_deterPerson_doDeterPerFun_posType;
function moduls_deterPerson_doDeterPerFun(id, userId, posType) {
	//moduls_deterPerson_propertyGrid = $('#moduls_deterPerson_propertyGrid').datagrid();
	moduls_deterPerson_doDeterPerFun_posType = posType;
	moduls_deterPerson_doDeterPerFun_id = id;
	moduls_deterPerson_doDeterPerFun_userId = userId;
	
	$('#moduls_deterPerson_propertyGrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	var index = $('#moduls_deterPerson_propertyGrid').datagrid('getRowIndex', id);
	$('#moduls_deterPerson_propertyGrid').datagrid('checkRow', index);
	
	if(!checkPosIdIs8()) {
		return;
	}
	
	//var crow = $('#moduls_deterPerson_propertyGrid').datagrid('getChecked');
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/doDeterPer'+(posType == 2 ? '' : 'Mark')+'.jsp',
		width : 720,
		height : $(window).height(),
		modal : true,
		title : '对个做测评',
		buttons : [ {
			text : '提交',
			iconCls : 'icon-ok',
			handler : function() {
				var d = $(this).closest('.window-body');
				moduls_deterPerson_doDeter();
			}
		}, {
			text : '取消',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).closest('.window-body').dialog('destroy');
		},
		onLoad : function() {
			moduls_deterPerson_doDeterPerFun_div = $(this);
			var rows = $('#moduls_deterPerson_propertyGrid').datagrid('getChecked');
			if(rows[0].personPic) {
				$('#fun_doDeterPer_img').attr('src', '${pageContext.request.contextPath}/imgs/photo/'+rows[0].personPic);
			} else {
				$('#fun_doDeterPer_img').attr("src", '${pageContext.request.contextPath}/imgs/photo/default.jpg');
			}
			
			$("#doDeterPer_username").text(rows[0].userRealName);
			$("#doDeterPer_posId").text(rows[0].posName);
			$("#doDeterPer_personSup").text(rows[0].supName);
			$("#doDeterPer_personSub").text(rows[0].subName);
			$("#doDeterPer_dutyObj").text(rows[0].dutyObj);
			$("#doDeterPer_detPerId").val(rows[0].userId);
		}
	});
}

function checkPosIdIs8() {
	var rows = $('#moduls_deterPerson_propertyGrid').datagrid('getChecked')[0];
	if(rows.posId == 8) {
		$.messager.show({
			title : '提示',
			msg : '不能测评院长和书记！'
		});
		return false;
	}
	return true;
}

function moduls_deterPerson_QueryDetDetailedFun(id, userId, posType, groupId, groupLeaderId) {
	if(deterPerson_QueryDetDetailed == 'A') {
		
	} else if(deterPerson_QueryDetDetailed == 'F') {
		if(!(yy.CUser.userId == groupLeaderId || yy.CUser.userId == userId)) {
			$.messager.show({
				title : '提示',
				msg : '权限不足！'
			});
			return;
		}
	} else if(deterPerson_QueryDetDetailed == 'O') {
		if(yy.CUser.groupId != groupId) {
			$.messager.show({
				title : '提示',
				msg : '权限不足！'
			});
			return;
		}
	} else {
		if(yy.CUser.userId != userId) {
			$.messager.show({
				title : '提示',
				msg : '权限不足！'
			});
			return;
		}
	}
	
	moduls_deterPerson_doDeterPerFun_id = id;
	moduls_deterPerson_doDeterPerFun_userId = userId;
	$('#moduls_deterPerson_propertyGrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	var index = $('#moduls_deterPerson_propertyGrid').datagrid('getRowIndex', id);
	$('#moduls_deterPerson_propertyGrid').datagrid('checkRow', index);
	
	if(!checkPosIdIs8()) {
		return;
	}
	
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/QueryDetDetailed'+(posType == 2 ? '' : 'Mark')+'.jsp',
		width : 720,
		height : $(window).height(),
		modal : true,
		title : '对个做测评',
		buttons : [ {
			text : '退出',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).closest('.window-body').dialog('destroy');
		},
		onLoad : function() {
			moduls_deterPerson_doDeterPerFun_div = $(this);
			var rows = $('#moduls_deterPerson_propertyGrid').datagrid('getChecked')[0];
			if(rows.personPic) {
				$('#fun_doDeterPer_img').attr('src', '${pageContext.request.contextPath}/imgs/photo/'+rows.personPic);
			} else {
				$('#fun_doDeterPer_img').attr("src", '${pageContext.request.contextPath}/imgs/photo/default.jpg');
			}
			
			$("#doDeterPer_username").text(rows.userRealName);
			$("#doDeterPer_posId").text(rows.posName);
			$("#doDeterPer_personSup").text(rows.supName);
			$("#doDeterPer_personSub").text(rows.subName);
			$("#doDeterPer_dutyObj").text(rows.dutyObj);
			$("#doDeterPer_detPerId").val(rows.userId);
		}
	});
}

function moduls_deterPerson_addFun() {
	$('#moduls_deterPerson_propertyGrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/addDeterPerson.jsp',
		width : 500,
		height : 250,
		modal : true,
		title : '添加测评者',
		toolbar:[{
			text:'添加照片',
			iconCls:'icon-add',
			handler:function(){
				 $('#moduls_fun_addDeterPerson_addForm input[name=pic]').trigger('click');
			}
		}],
		buttons : [ {
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				var d = $(this).closest('.window-body');
				
				$('#moduls_fun_addDeterPerson_addForm').form('submit', {
					url : '${pageContext.request.contextPath}/deterPersonAction!add.action',
					success : function(result) {
						var r = $.parseJSON(result);
						if (r.success) {
							$('#moduls_deterPerson_propertyGrid').datagrid('load');
							d.dialog('destroy');
						}
						$.messager.show({
							title : '提示',
							msg : r.msg
						});
					}
				});
			}
		}, {
			text : '取消',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
			var ids = [ '#moduls_fun_addDeterPerson_userId', '#moduls_fun_addDeterPerson_personSup', '#moduls_fun_addDeterPerson_personSub' ];

			for ( var i = 0; i < ids.length; i++) {
				$(ids[i]).combogrid({
					url : '${pageContext.request.contextPath}/userAction!queryUser.action',
					loadMsg : '数据加载中....',
					missingMessage : '请检索用户名',
					panelWidth : 440,
					panelHeight : 180,
					delay : 500,
					fitColumns : true,
					value : '',
					idField : 'userId',
					textField : 'userRealName',
					mode : 'remote',
					pagination : true,
					pageSize : 5,
					pageList : [ 5, 10 ],
					columns : [ [ {
						title : '编号',
						field : 'userId',
						width : 150,
						hidden : true
					}, {
						title : '工号',
						field : 'username',
						width : 80
					}, {
						title : '用户姓名',
						field : 'userRealName',
						width : 80
					}, {
						title : '所在组ID',
						field : 'groupId',
						width : 100,
						formatter : function(value, row, index) {
							if (value == 0)
								return '';
							return row.groupName;
						}
					}, {
						title : '角色ID',
						field : 'roleId',
						width : 100,
						formatter : function(value, row, index) {
							if (value == 0)
								return '';
							return row.roleName;
						}
					} ] ],
					onLoadSuccess : function(data) {
						if (data.rows.length == 1) {
							$(this).combogrid("grid").datagrid('selectRow', 0);
						}
					}
				});
			}
		}
	});
}

function moduls_deterPerson_editFun(id) {
	$('#moduls_deterPerson_propertyGrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	var index = $('#moduls_deterPerson_propertyGrid').datagrid('getRowIndex', id);
	$('#moduls_deterPerson_propertyGrid').datagrid('checkRow', index);
	$('<div/>').dialog({
		href : '${pageContext.request.contextPath}/moduls/fun/editDeterPerson.jsp',
		width : 500,
		height : 250,
		modal : true,
		title : '编辑测评项目',
		toolbar:[{
			text:'修改照片',
			iconCls:'icon-edit',
			handler:function(){
				 $('#moduls_fun_editDeterPerson_editForm input[name=pic]').trigger('click');
			}
		}],
		buttons : [ {
			text : '保存',
			iconCls : 'icon-save',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#moduls_fun_editDeterPerson_editForm').form('submit', {
					url : '${pageContext.request.contextPath}/deterPersonAction!modify.action',
					success : function(result) {
						try {
							var r = $.parseJSON(result);
							if (r.success) {
								$('#moduls_deterPerson_propertyGrid').datagrid('load');
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						} catch (e) {
							$.messager.alert('提示', result);
						}
					}
				});
			}
		}, {
			text : '取消',
			iconCls : 'icon-remove',
			handler : function() {
				$(this).closest('.window-body').dialog('destroy');
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
			var rows = $('#moduls_deterPerson_propertyGrid').datagrid('getChecked');
			if(rows.personPic) {
				$('#moduls_fun_editDeterPerson_editForm img[name=editDeterPerson_img]').attr('src', '${pageContext.request.contextPath}/imgs/photo/'+rows[0].personPic);
			} else {
				$('#moduls_fun_editDeterPerson_editForm img[name=editDeterPerson_img]').attr("src", '${pageContext.request.contextPath}/imgs/photo/default.jpg');
			}
			
			var ids = [ '#moduls_fun_addDeterPerson_userId', '#moduls_fun_addDeterPerson_personSup', '#moduls_fun_addDeterPerson_personSub' ];

			for ( var i = 0; i < ids.length; i++) {
				$(ids[i]).combogrid({
					url : '${pageContext.request.contextPath}/userAction!queryUser.action',
					loadMsg : '数据加载中....',
					missingMessage : '请检索用户名',
					panelWidth : 440,
					panelHeight : 180,
					delay : 500,
					fitColumns : true,
					value : '',
					idField : 'userId',
					textField : 'userRealName',
					mode : 'remote',
					pagination : true,
					pageSize : 5,
					pageList : [ 5, 10 ],
					columns : [ [ {
						title : '编号',
						field : 'userId',
						width : 150,
						hidden : true
					}, {
						title : '工号',
						field : 'username',
						width : 80
					}, {
						title : '用户姓名',
						field : 'userRealName',
						width : 80
					}, {
						title : '所在组ID',
						field : 'groupId',
						width : 100,
						formatter : function(value, row, index) {
							if (value == 0)
								return '';
							return row.groupName;
						}
					}, {
						title : '角色ID',
						field : 'roleId',
						width : 100,
						formatter : function(value, row, index) {
							if (value == 0)
								return '';
							return row.roleName;
						}
					} ] ],
					onLoadSuccess : function(data) {
						if (data.rows.length == 1) {
							$(this).combogrid("grid").datagrid('selectRow', 0);
						}
					}
				});
			}
			$('#moduls_fun_editDeterPerson_editForm').form('load', rows[0]);
			if(rows[0].personSup == 0) {
				$('#moduls_fun_addDeterPerson_personSup').combogrid('setValue','');
			}
			if(rows[0].personSub == 0) {
				$('#moduls_fun_addDeterPerson_personSub').combogrid('setValue','');
			}
			$('#moduls_fun_addDeterPerson_userId').combogrid('setText',rows[0].userRealName);
			$('#moduls_fun_addDeterPerson_personSup').combogrid('setText',rows[0].supName);
			$('#moduls_fun_addDeterPerson_personSub').combogrid('setText',rows[0].subName);
		}
	});
}

function moduls_deterPerson_deterStat() {
	$('<div/>').dialog({ 
		href : '${pageContext.request.contextPath}/moduls/fun/deterStat.html',
		width : 1000,
		height : 500,
		closable : false,
		modal : true,
		title : '测评统计',
	    modal:true,
	    buttons : [ {
			text : '退出',
			iconCls : 'icon-remove',
			handler : function() {
				try {
					$(this).closest('.window-body').dialog('destroy');
				} catch (e) {}
			}
		} ]
	});
	
}

function doModuls_deterPerson_deleteFun() {
	var rows = $('#moduls_deterPerson_propertyGrid').datagrid('getChecked');
	var ids = [];
	if (rows.length > 0) {
		$.messager.confirm('确认', '您是否要删除当前选中的项目？', function(r) {
			if (r) {
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].dpId);
				}
				$.ajax({
					url : '${pageContext.request.contextPath}/deterPersonAction!delete.action',
					data : {
						ids : ids.join(',')
					},
					dataType : 'json',
					success : function(result) {
						if (result.success) {
							$('#moduls_deterPerson_propertyGrid').datagrid('load');
							$('#moduls_deterPerson_propertyGrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
						}
						$.messager.show({
							title : '提示',
							msg : result.msg
						});
					}
				});
			}
		});
	} else {
		$.messager.show({
			title : '提示',
			msg : '请勾选要删除的记录！'
		});
	}
}

function moduls_deterPerson_deleteFun(id, text) {
	$('#moduls_deterPerson_propertyGrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
	var index = $('#moduls_deterPerson_propertyGrid').datagrid('getRowIndex', id);
	$('#moduls_deterPerson_propertyGrid').datagrid('checkRow', index);
	doModuls_deterPerson_deleteFun();
}
</script>
<table id="moduls_deterPerson_propertyGrid"></table>

<div id="moduls_deterPerson_datagrid_toolbar">
	<%if("admin".equalsIgnoreCase(cUserName)) {%>
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="javascript:moduls_deterPerson_addFun()">增加</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="doModuls_deterPerson_deleteFun()">批量删除</a>
	<%}%>

	<a id="groupMenu" href="javascript:void(0)" class="easyui-menubutton" data-options="menu:'#postList',iconCls:'icon-detailed'">按岗位测评</a>
	<%if(!("null".equals(queryDeterStat))) {%>
	<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-sum',plain:true" onclick="javascript:moduls_deterPerson_deterStat()">查看测评统计</a>
	<%}%>
</div>
<div id="postList" style="width:190px; height: 200px; overflow: auto;">  
</div>